/*
 * Android NewsFeed Application is a project for the Technological Educational Institute of Athens.
 * It's purpose is to familiarise the Android SDK and development procedures to students.
 * 
 * 
 * Android NewsFeed version 0.00
 * 
 * $LastChangedDate: 2011-08-11 14:48:40 +0300 (Thu, 11 Aug 2011) $
 * $Author: snoopaki $
 * $Rev: 40 $
 */
package gr.teiath.newsfeed;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
//import android.os.Debug;
import android.util.Log;

public class FetchService extends IntentService {

	private static final String tag ="NewsFeed-Service";	
	public static final String INTENT_HANDLE_NETWORK_CHANGE = "%&687^78%4$4&^9";
	private static NewsfeedApplication newsfeed;
	
	private static NotificationManager notificationMngr;
	private static Notification NOTIFY_NEW_ITEMS;
	private static Notification NOTIFY_RUN;
	private static final int NOTIFICATION_RUN_ID = 1;
	
	public FetchService() {
		super(tag);
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		
		Log.i(tag, "****** filterFeed // DEBUGGING TRACE");
		//Debug.startMethodTracing("FetchService");
		
		Log.i(tag, "****** Fetch Service // Service Run Starting!");
		newsfeed = (NewsfeedApplication) getApplication();
		
		if(intent != null) {
			final Bundle bundle = intent.getExtras();
			if(bundle != null && bundle.get(INTENT_HANDLE_NETWORK_CHANGE) != null) {
				newsfeed.handleNetworkChange();
			}
		}
		
		if(NewsfeedApplication.getNetworkConnectivity()) {
			notificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			NOTIFY_RUN = new Notification(android.R.drawable.stat_notify_sync, this.getString(R.string.notificationRunTicker), 0);
			this.notifyRun(NOTIFICATION_RUN_ID);
			
			int newPosts = newsfeed.fetchService();
			if(newPosts > 0) {
				NOTIFY_NEW_ITEMS = new Notification(android.R.drawable.sym_def_app_icon, this.getString(R.string.notificationNewItemsTicker, newPosts), 0);
				
				intent = new Intent(NewsList.NEWS_RECEIVER_INTENT);
				this.sendBroadcast(intent);
				this.notifyNewItems(newPosts);
			}
			killNotification(NOTIFICATION_RUN_ID);
		} else {
			//Toast.makeText(this, this.getText(R.string.noConnection), Toast.LENGTH_SHORT).show();
		}
		//Debug.stopMethodTracing();
		Log.i(tag, "****** Fetch Service // Service Run Ended...");		
	}

	
	private void notifyNewItems(int newPosts) {
		PendingIntent pendIntent = PendingIntent.getActivity(this, -1, new Intent(this, NewsList.class), PendingIntent.FLAG_UPDATE_CURRENT);
		
		NOTIFY_NEW_ITEMS.when = System.currentTimeMillis();
		NOTIFY_NEW_ITEMS.flags |= Notification.FLAG_AUTO_CANCEL;
		NOTIFY_NEW_ITEMS.defaults |= Notification.DEFAULT_SOUND;
		
		CharSequence notificationNewItemsTitle = this.getText(R.string.notificationNewItemsTitle);
		CharSequence notificationNewItemsSummary = this.getString(R.string.notificationNewItemsSummary, newPosts);
		
		NOTIFY_NEW_ITEMS.setLatestEventInfo(this, notificationNewItemsTitle, notificationNewItemsSummary, pendIntent);
		notificationMngr.notify(0, NOTIFY_NEW_ITEMS);
		Log.i(tag, "****** FetchService // New Items Notification Sent!");
	}
	
	private void notifyRun(int id) {
		PendingIntent pendIntent = PendingIntent.getActivity(this, -1, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
		
		NOTIFY_RUN.when = System.currentTimeMillis();
		NOTIFY_RUN.flags |= Notification.FLAG_ONGOING_EVENT;
		
		CharSequence notificationRunTitle = this.getText(R.string.notificationRunTitle);
		CharSequence notificationRunSummary = this.getString(R.string.notificationRunSummary);
		
		NOTIFY_RUN.setLatestEventInfo(this, notificationRunTitle, notificationRunSummary, pendIntent);
		notificationMngr.notify(id, NOTIFY_RUN);
		Log.i(tag, "****** FetchService // Running Notification Sent!");
	}
	
	private void killNotification(int id) {
		notificationMngr.cancel(id);
	}
	
}